﻿using System;
using System.Data;
using System.Data.Sql;
using System.Windows.Forms;

namespace CProd
{
    public partial class frmSql : Form
    {
        public frmSql()
        {
            InitializeComponent();
            cbDB.GotFocus += new EventHandler(cbDB_GotFocus);
        }

        private void frmSql_Load(object sender, EventArgs e)
        {
            chkLocal.Checked = Properties.Settings.Default.LocalChecked;
            Application.DoEvents();
            getSqlServers();
        }

        #region "Get Sql Servers"

        private void getSqlServers()
        {
            DataTable sqlServersTable = SqlDataSourceEnumerator.Instance.GetDataSources();

            foreach (DataRow rowOfData in sqlServersTable.Rows)
            {
                string serverName = rowOfData["ServerName"].ToString();
                string instanceName = rowOfData["InstanceName"].ToString();
                if (!instanceName.Equals(string.Empty))
                {
                    serverName = serverName + "\\" + instanceName;
                }
                cbServers.Items.Add(serverName);
            }
        }

        #endregion

        private void btConnect_Click(object sender, EventArgs e)
        {
            if (chkLocal.Checked)
            {
                Properties.Settings.Default.conStr = "data source=" + cbServers.SelectedItem + "; Initial Catalog="
                    + cbDB.SelectedItem + "; integrated security=sspi; connection timeout=7";
            }
            else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
            {
                Properties.Settings.Default.conStr = "data source=" + cbServers.SelectedItem + "; Initial Catalog="
                    + cbDB.SelectedItem + "; user id=" + txtUser.Text + "; password=" + txtPass.Text + ";connection timeout=7";
            }
            Properties.Settings.Default.Save();
            frmEntrada.conn.ConnectionString = Properties.Settings.Default.conStr;
            Close();
        }

        protected void cbDB_GotFocus(object sender, EventArgs e)
        {
            if (chkLocal.Checked)
                Properties.Settings.Default.conStr = "Data Source=" + cbServers.SelectedItem +
                    ";Integrated Security=SSPI; Connection Timeout=7";
            else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
                Properties.Settings.Default.conStr = "Data Source=" + cbServers.SelectedItem +
                    "; User ID=" + txtUser.Text + "; Password=" + txtPass.Text + ";Connection Timeout=7";
            frmEntrada.conn.ConnectionString = Properties.Settings.Default.conStr;
            GetDatabases();
        }

        protected void GetDatabases()
        {
            try
            {
                frmEntrada.conn.Open();
                DataTable tblDatabases = frmEntrada.conn.GetSchema("Databases");
                foreach (DataRow row in tblDatabases.Rows) cbDB.Items.Add(row["database_name"].ToString());
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            frmEntrada.conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Form frm in Application.OpenForms)
                frm.Close();
            Application.Exit();
        }

    }
}
